<?php
	require_once(dirname(dirname(__FILE__))) . '/classes/cAmpleHarvestLog.php';

	class abs_Task
	{
		protected $m_obj_Log = null;

		private $m_str_Name = "default";
		private $m_bool_Force = false;
		private $m_bool_Active = false;
		private $m_str_LockPath = "";
		private $m_str_Thread = "";

		private static $m_obj_DataFile = null;

		private $m_int_MaxThreads = 1;

		public function get_LockFilePath()
		{
			return $this->m_str_LockPath . "/" . $this->m_str_Name . "_" . $this->m_str_Thread . ".lock";
		}

		public function get_TrackFilePath()
		{
			return $this->m_str_LockPath . "/tracking/" . $this->m_str_Name . ".txt";
		}

		public function __construct()
		{
			$this->m_obj_Log = new \cAmpleHarvestLog();
			$this->set_MaxThreads(1);

			$this->set_DataFileProp("start", date_format(new \DateTime(), "c"));
		}

		protected function set_DataFileProp($str_Prop, $str_Value, $bool_ThreadSpec = true)
		{
			$str_Thread = "thread_" . $this->m_str_Thread;
			if (!isset(self::$m_obj_DataFile))
			{
				self::$m_obj_DataFile = array();
			}

			if (!isset(self::$m_obj_DataFile[$this->m_str_Name]))
			{
				self::$m_obj_DataFile[$this->m_str_Name] = new \stdClass();
			}
			if ($bool_ThreadSpec && $this->m_str_Thread)
			{
				if (!isset(self::$m_obj_DataFile[$this->m_str_Name]->$str_Thread))
				{
					self::$m_obj_DataFile[$this->m_str_Name]->$str_Thread = new \stdClass();
				}
			}
			$obj_DataFile = self::$m_obj_DataFile;

			if ($bool_ThreadSpec && $this->m_str_Thread)
			{
				$obj_DataFile[$this->m_str_Name]->$str_Thread->$str_Prop = $str_Value;
			}
			else
			{
				$obj_DataFile[$this->m_str_Name]->$str_Prop = $str_Value;
			}

		}

		public function set_ThreadID($str_ThreadID)
		{
			$this->m_str_Thread = $str_ThreadID;
			$this->set_DataFileProp("start", date_format(new \DateTime(), "c"));
		}

		public function set_MaxThreads($int_Max)
		{
			$this->m_int_MaxThreads = $int_Max;
			$this->set_DataFileProp("maxThreads", $int_Max);
		}
		public function get_MaxThreads()
		{
			return $this->m_int_MaxThreads ;
		}

		public function __destruct()
		{
			if ($this->m_bool_Active)
			{
				if ($this->isLocked())
				{
					if (!unlink($this->get_LockFilePath()))
					{
						$this->log_Error("Failed to remove Lock File at " . $this->get_LockFilePath());
					}
				}

				$this->set_DataFileProp("end", date_format(new \DateTime(), "c"));
				$this->createTrack();
				$this->log("Finished ");
			}
		}

		public function run()
		{
			if ($this->isLocked() && !$this->m_bool_Force)
			{
				$this->log_Error("Failed to start lock file is present at " . $this->get_LockFilePath());
				return false;
			}
			$this->log("Starting");
			$this->createLock();
			$this->m_bool_Active = true;
			return true;
		}

		protected function log($str_Msg, $int_LogLevel = cAmpleHarvestLog::C_INT_LEVEL_INFO)
		{
			$this->m_obj_Log->do_Log(cAmpleHarvestLog::C_STR_ACTION_TASK_INFO,
				"Task - " . $this->m_str_Name . " - " . $str_Msg,
				$int_LogLevel);
		}

		protected function log_Error($str_Msg, $int_LogLevel = cAmpleHarvestLog::C_INT_LEVEL_ERRORS)
		{
			$this->m_obj_Log->do_Log(cAmpleHarvestLog::C_STR_ACTION_TASK_ERROR,
				"Task - " . $this->m_str_Name . " - " . $str_Msg,
				$int_LogLevel);
		}

		protected function createLock()
		{
			$obj_FileHandler = fopen($this->get_LockFilePath(), "w");
			if ($obj_FileHandler)
			{
				fwrite($obj_FileHandler, date("F d, Y H:i:s") . " - Lock Created \n");

				fclose($obj_FileHandler);
			}
			else
			{
				$this->log_Error("Failed to create Lock file at " . $this->get_LockFilePath());
			}
		}

		protected function createTrack()
		{
			$obj_FileHandler = fopen($this->get_TrackFilePath(), "w");
			if ($obj_FileHandler)
			{
				fwrite($obj_FileHandler, json_encode(self::$m_obj_DataFile[$this->m_str_Name]));

				fclose($obj_FileHandler);
			}
			else
			{
				$this->log_Error("Failed to create Track file at " . $this->get_TrackFilePath());
			}
		}

		public function set_Name($str_Name)
		{
			$this->m_str_Name = $str_Name;
		}

		public function get_Name()
		{
			return $this->m_str_Name ;
		}

		public function set_Force($bool_Force = false)
		{
			$this->m_bool_Force = $bool_Force;
		}

		public function set_LockPath($str_Path)
		{
			$this->m_str_LockPath = $str_Path;
		}

		protected function isLocked()
		{
			return file_exists($this->get_LockFilePath());
		}
	}
?>